package net.huashitong.zhApp.service;

import com.sirdc.modules.sys.filter.NodeNewschedulesModel;
import com.sirdc.modules.sys.service.PlanTimeReckonService;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewschedulesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

@Service
public class AppReportService {

    @Autowired
    private PlanTimeReckonService planTimeReckonService;
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private AppProjectsService appProjectsService;


    public List<NodeNewschedulesModel> getNodeDetailByProjectId(String projectId, String nodeId) throws ParseException {
        List<NodeNewschedulesModel> models = new ArrayList<>();
        // state:2 == 不需要(Dao层条件是'!=')
        List<NodeNewschedules> nodeNewschedules = nodeNewschedulesService.getNodeByProjectIdAndState(projectId, "");
        NewProjects projects = newProjectsService.getById(projectId);
        //各个节点的前置节点
        Map<String, Object> preOfNodeMap = new TreeMap<>();
        for (NodeNewschedules n : nodeNewschedules) {
            //筛选确定的节点
            if ("1".equals(n.getState())) {
                //大节点
                if ("1".equals(n.getType())) {
                    if (!preOfNodeMap.containsKey(n.getNodeId())) {
                        preOfNodeMap.put(n.getNodeId(), n.getPreNodeId());
                    }
                } else {
                    //小节点
                    if (!preOfNodeMap.containsKey(n.getNodeId())) {
                        preOfNodeMap.put(n.getNodeId(), preOfNodeMap.get(n.getNodeIds()));
                    }
                }
            }
            if (StringUtils.isNotBlank(n.getFactTime()) && StringUtils.isNotBlank(n.getPlanTime())) {
                if (!"2".equals(n.getStatus())) {
                    n.setIsDelay("-1");     //未完成
                    continue;
                }
                int factTime = Integer.parseInt(n.getFactTime());
                int planTime = Integer.parseInt(n.getPlanTime());
                if (factTime > planTime) {        //实际时间大于计划时间----逾期
                    n.setIsDelay("1");
                } else if (factTime < planTime) {  //实际时间大于计划时间----超前
                    n.setIsDelay("2");
                } else if (factTime == planTime) { //实际时间大于计划时间----正常
                    n.setIsDelay("0");
                }
            }
            n.setCreateDate(projects.getCreateDate());

        }
        for (NodeNewschedules n : nodeNewschedules) {
            NodeNewschedulesModel m = new NodeNewschedulesModel();
            //对象复制n->m
            BeanUtils.copyProperties(n, m);
            models.add(m);
        }
        //推算计划时间
        models = planTimeReckonService.reckonPlanTime(models, preOfNodeMap);
        //批量更新时间状态
//        nodeNewschedulesService.batchUpdateObject(nodeList);
        List<NodeNewschedulesModel> bigNodeModel = new ArrayList<>();       //大节点
        List<NodeNewschedulesModel> smallNodeModel = new ArrayList<>();     //小节点
        List<NodeNewschedulesModel> noUseNodeModel = new ArrayList<>();     //无用节点
        List<NodeNewschedulesModel> resultNodeModel = new ArrayList<>();     //无用节点

        int factUseTime = 0;    //实际用时
        int planUseTime = 0;    //计划用时
        int factUseTimeAll = 0;    //所有实际用时
        int planUseTimeAll = 0;    //所有计划用时

        StringBuffer strBufFinish = new StringBuffer();
        StringBuffer strBufNoFinish = new StringBuffer();
        StringBuffer strBufBegin = new StringBuffer();

        int noFinishNodeNum = 0;         //未完成节点个数
        int doingNodeNum = 0;            //进行中节点个数
        int finishedNodeNum = 0;         //已完成节点个数
        models = nodeNewschedulesService.addNextNodeDealWith(models);
        models = appProjectsService.dealWithNodes2(models);
        for (NodeNewschedulesModel m : models) {
            if ("1".equals(m.getState()) && "1".equals(m.getType())) {
                //确定的大节点
                if (StringUtils.isBlank(m.getPlanBeginDate()) && StringUtils.isBlank(m.getPlanBeginTime())) {
//                    noUseNodeModel.add(m);
//                    continue;
                }
                bigNodeModel.add(m);
                if ("2".equals(m.getStatus())) {
                    strBufFinish.append(m.getNodeId() + ";");
                    factUseTime = factUseTime + Integer.parseInt(StringUtils.isBlank(m.getFactTime()) ? "0" : m.getFactTime());
                    finishedNodeNum++;
                } else {
                    noFinishNodeNum++;
                    strBufNoFinish.append(m.getNodeId() + ";");
                    planUseTime = planUseTime + Integer.parseInt(StringUtils.isBlank(m.getPlanTime()) ? "0" : m.getPlanTime());
                }
                factUseTimeAll = factUseTimeAll + Integer.parseInt(StringUtils.isBlank(m.getFactTime()) ? "0" : m.getFactTime());
                planUseTimeAll = planUseTimeAll + Integer.parseInt(StringUtils.isBlank(m.getPlanTime()) ? "0" : m.getPlanTime());
            } else if ("1".equals(m.getState()) && "2".equals(m.getType())) {
                //确定的小节点
                if (StringUtils.isBlank(m.getPlanBeginDate()) && StringUtils.isBlank(m.getPlanBeginTime())) {
//                    noUseNodeModel.add(m);
//                    continue;
                }
                if ("1".equals(m.getStatus())) {
                    strBufBegin.append(m.getNodeId() + ";");
                    doingNodeNum++;
                }
                smallNodeModel.add(m);
            } else {
                noUseNodeModel.add(m);
            }
        }
        //排序实际开始时间正序排，如果没有实际开始时间则取计划开始时间
        Collections.sort(bigNodeModel, new Comparator<NodeNewschedulesModel>() {
            @Override
            public int compare(NodeNewschedulesModel m1, NodeNewschedulesModel m2) {
                String factBegin1 = m1.getPlanBeginDate();//实际开始时间
                if (StringUtils.isBlank(factBegin1)) {
                    if (StringUtils.isBlank(m1.getPlanBeginTime())) {
                        return 1;
                    }
                    factBegin1 = m1.getPlanBeginTime();//计划开始时间
                }
                long longFactBegin1 = Long.valueOf(factBegin1.replace(".", ""));

                String factBegin2 = m2.getPlanBeginDate();//实际开始时间
                if (StringUtils.isBlank(factBegin2)) {
                    if (StringUtils.isBlank(m2.getPlanBeginTime())) {
                        return 1;
                    }
                    factBegin2 = m2.getPlanBeginTime();//计划开始时间
                }
                long longFactBegin2 = Long.valueOf(factBegin2.replace(".", ""));

                if (longFactBegin1 < longFactBegin2) {
                    return -1;
                } else if (longFactBegin1 == longFactBegin2) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });

        //排出每个节点的下个节点
        for (NodeNewschedulesModel x : bigNodeModel) {
            for (NodeNewschedulesModel y : bigNodeModel) {
                String preNodes = y.getPreNodeId();
                if (preNodes.contains(",")) {
                    String[] preNodesArr = preNodes.split(",");
                    for (String s : preNodesArr) {
                        if (x.getNodeId().equals(s)) {
                            String val = x.getNextNodes();
                            if (StringUtils.isBlank(val)) {
                                x.setNextNodes(y.getNodeId() + " " + y.getNodeName());
                            } else {
                                x.setNextNodes(val + "," + y.getNodeId() + " " + y.getNodeName());
                            }
                        }
                    }
                } else {
                    String preNode = preNodes;
                    if (x.getNodeId().equals(preNode)) {
                        String val = x.getNextNodes();
                        if (StringUtils.isBlank(val)) {
                            x.setNextNodes(y.getNodeId());
                        } else {
                            x.setNextNodes(val + "," + y.getNodeId());
                        }
                    }
                }
            }
            if (StringUtils.isBlank(x.getNextNodes())) {
                x.setNextNodes("-");
            }
        }

        if (StringUtils.isNotBlank(nodeId)) {
            for (NodeNewschedulesModel big : bigNodeModel) {
                if (nodeId.equals(big.getNodeId())) {
                    resultNodeModel.add(big);
                    for (NodeNewschedulesModel small : smallNodeModel) {
                        if (big.getNodeId().equals(small.getNodeIds())) {
                            resultNodeModel.add(small);
                        }
                    }
                }
            }
        } else {
            for (NodeNewschedulesModel big : bigNodeModel) {
                resultNodeModel.add(big);
                for (NodeNewschedulesModel small : smallNodeModel) {
                    if (big.getNodeId().equals(small.getNodeIds())) {
                        resultNodeModel.add(small);
                    }
                }
            }
            //节点最终结果
            resultNodeModel.addAll(noUseNodeModel);
        }
        return resultNodeModel;
    }
}
